﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;
using System.Web.Services;
using System.Data.Objects.DataClasses;

namespace WebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        //-------------------------------------------chauffeur--------------------------------------------------------------------------
        //permet de verifier l'existance  d'un chauffeur


        [WebMethod]
        public Chauffeur ConnxionChauffeur(string log, string pwd)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var chauff = (from x in cxt.Chauffeur where ((x.Login == log) && (x.Password == pwd)) select x).FirstOrDefault();
                if (chauff != null)
                    return chauff;
                else
                    return null;
            }
        }
        //permet d'ajouter un chauffeur
        [WebMethod]
        public bool AjouterChauffeur(string n, string p, string a, string t, string e, string i, string l, string pwd)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var chauff = new Chauffeur { Nom = n, Prenom = p, Adresse = a, Tel = t, Email = e, Image = i, Login = l, Password = pwd };
                cxt.AddToChauffeur(chauff);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de suprimer un chauffeur à l'aide de son id
        [WebMethod]
        public bool SupprimerChauffeur(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unchauff in cxt.Chauffeur
                              where (unchauff.id_chauffeur == id)
                              select unchauff).First();
                if (result != null)
                {
                    cxt.DeleteObject(result);
                }

                if (cxt.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        //permet de modifier un chauffeur
        [WebMethod]
        public bool ModifierChauffeur(string n, string p, string a, string t, string l, string pass, string i, string e, int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unchauff in cxt.Chauffeur where (unchauff.id_chauffeur == id) select unchauff).First();
                if (result != null)
                {
                    result.Nom = n;
                    result.Prenom = p;
                    result.Adresse = a;
                    result.Tel = t;
                    result.Login = l;
                    result.Password = pass;
                    result.Image = i;
                    result.Email = e;
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de consulter un chaffeur
        [WebMethod]
        public Chauffeur ConsulterChauffeur(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from chauff in cxt.Chauffeur
                              where chauff.id_chauffeur == id
                              select chauff).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        //permet d'afficher la liste des chaffeurs
        [WebMethod]
        public List<Chauffeur> AfficherListeChauffeur()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from leschauff in cxt.Chauffeur select leschauff).ToList<Chauffeur>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }


        //-------------------------------------------employee--------------------------------------------------------------------------




        //permet d'ajouter un employee
        [WebMethod]
        public bool Ajouteremployee(string n, string p, string a, string t, string e, string i, string l, string pwd, int ty)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var employ = new Employer { Nom = n, Prenom = p, Adresse = a, Tel = t, Email = e, Image = i, Login = l, Password = pwd, Type = ty };
                cxt.AddToEmployer(employ);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de suprimer un employee à l'aide de son id
        [WebMethod]
        public bool SupprimerEmployer(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unemploy in cxt.Employer
                              where (unemploy.id_employer == id)
                              select unemploy).First();
                if (result != null)
                {
                    cxt.DeleteObject(result);
                }

                if (cxt.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        //permet de modifier un employee
        [WebMethod]
        public bool ModifierEmployer(string n, string p, string a, string t, string l, string pass, string i, string e, int id, int ty)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unemploy in cxt.Employer where (unemploy.id_employer == id) select unemploy).First();
                if (result != null)
                {
                    result.Nom = n;
                    result.Prenom = p;
                    result.Adresse = a;
                    result.Tel = t;
                    result.Login = l;
                    result.Password = pass;
                    result.Image = i;
                    result.Email = e;
                    result.Type = ty;
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de consulter un employee
        [WebMethod]
        public Employer ConsulterEmployer(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from Employ in cxt.Employer
                              where Employ.id_employer == id
                              select Employ).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        //permet d'afficher la liste des employees
        [WebMethod]
        public List<Employer> AfficherListeEmployer()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from lesEmployers in cxt.Employer select lesEmployers).ToList<Employer>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }



        //-------------------------------------------Vehicule--------------------------------------------------------------------------




        //permet d'ajouter un Vehicule
        [WebMethod]
        public bool AjouterVehicule(string mat, string mar)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var veh = new Vehicule { Matricule = mat, Marque = mar };
                cxt.AddToVehicule(veh);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de suprimer un Vehicule à l'aide de son id
        [WebMethod]
        public bool SupprimerVehicule(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unveh in cxt.Vehicule
                              where (unveh.id_vehicule == id)
                              select unveh).First();
                if (result != null)
                {
                    cxt.DeleteObject(result);
                }

                if (cxt.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        //permet de modifier un Vehicule
        [WebMethod]
        public bool ModifierVehicule(string mat, string mar, int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unveh in cxt.Vehicule where (unveh.id_vehicule == id) select unveh).First();
                if (result != null)
                {
                    result.Marque = mar;
                    result.Matricule = mat;


                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de consulter un Vehicule
        [WebMethod]
        public Vehicule ConsulterVehicule(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from veh in cxt.Vehicule
                              where veh.id_vehicule == id
                              select veh).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        //permet d'afficher la liste des véhicules
        [WebMethod]
        public List<Vehicule> AfficherListeVehicule()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from lesVehicules in cxt.Vehicule select lesVehicules).ToList<Vehicule>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }


        //-------------------------------MISSION-------------------------------------------
        //permet d'ajouter une mission
        [WebMethod]
        public bool AjouterMission(string l, int id_chauff, int id_rt, int veh, string dat_dec, string dat_f_est)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {   
                
                DateTime D1 = Convert.ToDateTime(dat_dec);
                DateTime D2 = Convert.ToDateTime(dat_f_est);

                DateTime dat_1_notif= D1.AddDays(-1);// avant un jour
                DateTime dat_2_notif = D1.AddHours(-2); // avant 2 heures


                var miss = new Mission
                {
                    libellé = l,
                    Etat ="En attente",
                    id_chauffeur = id_chauff,
                    id_route = id_rt,
                    id_vehicule = veh,
                    DateDeclanchement = D1,
                    DateFinEstimé = D2,
                    DatePremierNotif = dat_1_notif,
                    DateDeuxiemeNotif = dat_2_notif
                };


                cxt.AddToMission(miss);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }


        //permet d'afficher la liste des missions 
        [WebMethod]
        public List<Mission> AfficherListeMissions()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var res = (from Miss in cxt.Mission select Miss ).ToList<Mission>();
                if (res != null)
                    return res;
                else
                    return null;
            }
        }

        //permet de consulter une mission 
        [WebMethod]
        public Mission ConsulterMission(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from miss in cxt.Mission
                              where miss.id_mission == id
                              select miss).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        //permet de suprimer une mission à l'aide de son id
        [WebMethod]
        public bool SupprimerMission(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unMiss in cxt.Mission
                              where (unMiss.id_mission == id)
                              select unMiss).First();
                if (result != null)
                {
                    cxt.DeleteObject(result);
                }

                if (cxt.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    //permet de modifier une mission
        [WebMethod]
        public bool Modifier_Mission(int id,int id_chauff,int id_r, int veh, string dat_d_r, string dat_f_r, string Et)
        {
            DateTime Dat = Convert.ToDateTime(dat_d_r);
            DateTime Dat2 = Convert.ToDateTime(dat_f_r);
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unMiss in cxt.Mission where (unMiss.id_mission == id) select unMiss).First();
                if (result != null)
                {

                    result.DateDebutReelle = Dat;
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }

        //permet de cloturer une mission
        [WebMethod]
        public bool Cloturer_Mission(string Dat_f_r, int id)
        {
            DateTime Dat = Convert.ToDateTime(Dat_f_r);
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unMiss in cxt.Mission where (unMiss.id_mission == id) select unMiss).First();
                if (result != null)
                {

                    result.DateDebutReelle = Dat;
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }

        //permet de lister les notifications 
        [WebMethod]
        public List<Notification> AfficherListeNotifications()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var res = (from Notif in cxt.Notification select Notif).ToList<Notification>();
                if (res != null)
                    return res;
                else
                    return null;
            }
        }

        








    }
}